import os

from django.conf import settings
from django.template import loader

from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories


def generate_static_index_html():
    contents = {}
    content_categories = ContentCategory.objects.all()
    for cat in content_categories:
        contents[cat.key] = cat.content_set.all()
    context = {
        'categories': get_categories(),
        'contents': contents
    }
    template=loader.get_template('index.html')
    html_text=template.render(context)
    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)